package com.xiaohudie.JDBC;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.sql.*;
import java.util.Properties;

/**
 * @Author xiao hu die
 * @Date 2022/3/12 22:00
 * @Version 1.0
 */
public class TestJdbcProperties {
    public static void main(String[] args) throws IOException {
        Properties properties =new Properties();
        properties.load(new FileReader("src//dp.properties"));
        Connection connection =null;
        Statement statement =null;
        ResultSet resultSet =null;
        String url = properties.getProperty("url");
        String root = properties.getProperty("user");
        String password =properties.getProperty("password");
        String driver =properties.getProperty("driver");
        try {
            /**注册驱动*/
            Class.forName(driver);
            /**获取连接*/
            try {
                connection = DriverManager.getConnection(url, root, password);
                /**获取数据库操作对象*/
                statement = connection.createStatement();
                /**得到sql语句*/
                String InsertSql= "INSERT INTO user (id,name,phone) VALUES (8,'陈唯怡','13695478526')";
                /**执行sql语句*/
                int i = statement.executeUpdate(InsertSql);
                System.out.println(i);
            } catch (SQLException e) {
                e.printStackTrace();
            }

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }finally {
            if (resultSet!=null){
                try {
                    resultSet.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (statement !=null){
                try {
                    statement.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (connection!=null){
                try {
                    connection.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }

        }
    }

}

